package com.yanqu.road.dao.impl.activity.huashigang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangPatrons;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/10/11
 */
public class HuaShiGangUserDataDaoImpl extends TempDataObjectDao<HuaShiGangUserData> {

    public static String getHuaShiGangUserDataSql = "select * from t_c_huashigang_user_data where activityId=?";
    public Map<Long, HuaShiGangUserData> getHuaShiGangUserData(long activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, HuaShiGangUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHuaShiGangUserDataSql);
            int i = 1;
            call.setLong(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangUserData huaShiGangUserData = parse(rs);
                map.put(huaShiGangUserData.getUserId(), huaShiGangUserData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }



    public static String updateHuaShiGangUserDataSql = "update t_c_huashigang_user_data set x=?,y=?,isFinish=?,monsterId=?,allScore=?,randomPatronsId=?,boxMap=?,patronsMap=?,resetUpdateTime=?,scoreUpdateTime=?,boxEventId=?,userBaseInfo=?,transportId=?,transportList=?,openSwitchList=?,totalAbility=? where activityId=? and serverId=? and userId=?";
    @Override
    protected boolean updateData(HuaShiGangUserData huaShiGangUserData) {
        huaShiGangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getY()));
        para.put(i++, new DbParameter(Types.BOOLEAN, huaShiGangUserData.isFinish()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getAllScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getRandomPatronsId())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getBoxMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getResetUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getScoreUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getBoxEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getTransportId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getTransportList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getOpenSwitchList())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getTotalAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getUserId()));
        if (getDb().executeNoneQuery(updateHuaShiGangUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHuaShiGangUserDataSql = "insert into t_c_huashigang_user_data(activityId,serverId,userId,x,y,isFinish,monsterId,allScore,randomPatronsId,boxMap,patronsMap,resetUpdateTime,scoreUpdateTime,boxEventId,userBaseInfo,transportId,transportList,openSwitchList,totalAbility) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(HuaShiGangUserData huaShiGangUserData) {
        huaShiGangUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getY()));
        para.put(i++, new DbParameter(Types.BOOLEAN, huaShiGangUserData.isFinish()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getMonsterId()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getAllScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getRandomPatronsId())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getBoxMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getPatronsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getResetUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getScoreUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getBoxEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, huaShiGangUserData.getTransportId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getTransportList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(huaShiGangUserData.getOpenSwitchList())));
        para.put(i++, new DbParameter(Types.BIGINT, huaShiGangUserData.getTotalAbility()));
        if (getDb().executeNoneQuery(insertHuaShiGangUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HuaShiGangUserData parseData(ResultSet rs) throws SQLException {HuaShiGangUserData huaShiGangUserData = new HuaShiGangUserData();
        huaShiGangUserData.setActivityId(rs.getLong("activityId"));
        huaShiGangUserData.setServerId(rs.getLong("serverId"));
        huaShiGangUserData.setUserId(rs.getLong("userId"));
        huaShiGangUserData.setX(rs.getInt("x"));
        huaShiGangUserData.setY(rs.getInt("y"));
        huaShiGangUserData.setIsFinish(rs.getBoolean("isFinish"));
        huaShiGangUserData.setMonsterId(rs.getInt("monsterId"));
        huaShiGangUserData.setAllScore(rs.getLong("allScore"));
        huaShiGangUserData.setRandomPatronsId(JSON.parseObject(rs.getString("randomPatronsId"), new TypeReference<List<Integer>>() {}));
        huaShiGangUserData.setBoxMap(JSON.parseObject(rs.getString("boxMap"), new TypeReference<Map<Integer, Integer>>() {}));
        huaShiGangUserData.setPatronsMap(JSON.parseObject(rs.getString("patronsMap"), new TypeReference<Map<Integer, HuaShiGangPatrons>>() {}));
        huaShiGangUserData.setResetUpdateTime(rs.getLong("resetUpdateTime"));
        huaShiGangUserData.setScoreUpdateTime(rs.getLong("scoreUpdateTime"));
        huaShiGangUserData.setBoxEventId(rs.getInt("boxEventId"));
        huaShiGangUserData.setTransportId(rs.getInt("transportId"));
        huaShiGangUserData.setTransportList(JSON.parseObject(rs.getString("transportList"), new TypeReference<List<Integer>>() {}));
        String openSwitchList = rs.getString("openSwitchList");
        if (!StringUtils.isNullOrEmpty(openSwitchList)) {
            huaShiGangUserData.setOpenSwitchList(JSON.parseObject(openSwitchList, new TypeReference<List<Integer>>() {}));
        }
        UserBaseInfo userBaseInfo;
        String userBaseInfoStr = rs.getString("userBaseInfo");
        if (StringUtils.isNullOrEmpty(userBaseInfoStr)) {
            userBaseInfo = new UserBaseInfo();
        } else {
            userBaseInfo = JSON.parseObject(userBaseInfoStr, UserBaseInfo.class);
        }
        huaShiGangUserData.setUserBaseInfo(userBaseInfo);
        huaShiGangUserData.setTotalAbility(rs.getLong("totalAbility"));
        return huaShiGangUserData;
    }
}
